package com.irdstudio.apicenter.console.controller.authenticate;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.AppDic;
import com.irdstudio.apicenter.console.service.authenticate.AppDicService;

@Controller
@RequestMapping(value = "/appDic")
public class AppDicAction extends MacmsBaseAction {
	@Autowired
	private AppDicService appDicService;

	// 插入跳转
	@RequestMapping(value = "jumpAddDic")
	public String jumpAddDic(HttpServletRequest request) {
		return "auth/addDic";
	}

	// 插入保存
	@RequestMapping(value = "addDic")
	public void addDic(@ModelAttribute("appDic") AppDic appdic,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appDicService.insertOne(appdic);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 更新跳转
	@RequestMapping(value = "jumpUpdateDic")
	public String jumpUpdateDic(HttpServletRequest request)
			throws SQLException, ClassNotFoundException {
		String recordKey = request.getParameter("recordKey");
		AppDic appDic = appDicService.queryAppTradePrivate(recordKey);
		request.setAttribute("appDic", appDic);
		return "auth/updateDic";
	}

	// 更新保存
	@RequestMapping(value = "updateDic")
	public void updateDic(@ModelAttribute("appDic") AppDic appDic,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appDicService.update(appDic);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 删除
	@RequestMapping(value = "deleteDic")
	public void deleteDic(HttpServletRequest request,
			HttpServletResponse response) throws SQLException,
			ClassNotFoundException {
		int flag = -1;
		String recordKey = request.getParameter("recordKey");
		flag = appDicService.delete(recordKey);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 查看
	@RequestMapping(value = "queryDicDetail")
	public String queryDicDetail(HttpServletRequest request) throws SQLException,
			ClassNotFoundException {
		String recordKey = request.getParameter("recordKey");
		AppDic appDic = appDicService.queryAppTradePrivate(recordKey);
		request.setAttribute("appDic", appDic);
		return "auth/queryDicDetail";
	}

}
